package mq.rabbitmq.confirm;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmListener;
import mq.rabbitmq.RabbitMqChannelFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import static mq.rabbitmq.Constant.CONFIRM_EX_CHANGE;
import static mq.rabbitmq.Constant.CONFIRM_ROUTING_KEY;

/**
 * 生产者
 *
 * @author verlet
 * @date 2019/9/11
 */
public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        // 声明channel
        Channel channel = RabbitMqChannelFactory.createChannel();
        // 设置成confirm模式
        channel.confirmSelect();

        // 发布消息
        channel.basicPublish(CONFIRM_EX_CHANGE, CONFIRM_ROUTING_KEY, null, "confirm模式".getBytes());
        System.out.println("发布一条消息成功。");

        // 添加confirm模式监听
        channel.addConfirmListener(new ConfirmListener() {
            @Override
            public void handleAck(long deliveryTag, boolean multiple) {
                System.out.println("=====ACK======>deliveryTag=" + deliveryTag + ";multiple=" + multiple);
            }

            @Override
            public void handleNack(long deliveryTag, boolean multiple) {
                System.out.println("=====NOT ACK======>deliveryTag=" + deliveryTag + ";multiple=" + multiple);
            }
        });
        Thread.sleep(1000);
        // 关闭连接
        RabbitMqChannelFactory.close(channel);
    }
}